<script setup>
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";
import { useRoute } from "vue-router";
import {ref} from "vue";
const route = useRoute()
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openDelete
} = useTable({
  requestFn: http.MemberAddress,
  params: {
    member_id: route.params.id
  }
})
const dialogVisible = ref(false)
const formModel = ref({
  username: '',
  phone: '',
  address: '',
  code: ''
})
const rules = ref({
  username: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入地址', trigger: 'blur' }
  ]
})
const formRef = ref(null)
const handleConfirm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (formModel.value.id) {
        http.MemberAddress.update({
          id: formModel.value.id,
          member_id: route.params.id,
          ...formModel.value
        }).then(() => {
          dialogVisible.value = false
          search()
        })
      } else {
        http.MemberAddress.create({
          member_id: route.params.id,
          ...formModel.value
        }).then(() => {
          dialogVisible.value = false
          search()
        })
      }
    }
  })
}
const openAdd = () => {
  dialogVisible.value = true
  formModel.value = {
    username: '',
    phone: '',
    address: '',
    code: ''
  }
}
const openEdit = (row) => {
  formModel.value = {
    id: row.id,
    username: row.username,
    phone: row.phone,
    address: row.address,
    code: row.code
  }
}
const changeStatus = (row) => {
  http.MemberAddress.updateStatus({
    id: row.id,
    member_id: route.params.id,
  }).then(() => {
    search()
  })
}
</script>


<template>
  <PageMain title="会员地址管理" is-back>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="姓名" prop="username">
        </el-table-column>
        <el-table-column label="手机" prop="phone">
        </el-table-column>
        <el-table-column label="默认状态">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-switch :active-value="1" :inactive-value="0" :disabled="scope.row.is_default" v-model="scope.row.is_default" @change="changeStatus(scope.row)"></el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
    <el-dialog
        v-model="dialogVisible"
        title="地址"
        width="500"
    >
      <el-form label-width="80px" ref="formRef" :model="formModel" :rules="rules">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formModel.username"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="formModel.phone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formModel.address" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="门牌号" prop="code">
          <el-input v-model="formModel.code"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirm">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
